package cn.eclass.bean;

import java.util.ArrayList;
import java.util.List;

import cn.eclass.bean.api.ISchoolLinkBean;
import cn.eclass.dao.SchoolLinkDao;
import cn.eclass.entity.SchoolLinkEntity;
import cn.eclass.utils.Page;

public class SchoolLinkBean implements ISchoolLinkBean {

    private SchoolLinkDao dao = new SchoolLinkDao();

//	HashMap<Integer, ArrayList<SchoolLinkentity>> schoolLinksMap = null;
    //pageNo starts from 1;
    @Override
    public List<SchoolLinkEntity> getSchoolLinkByCat(int catId, int itemPerPage, int pageNo) {


        ArrayList<Integer> catIdList = new ArrayList<Integer>();
        new CategoryBean().getListFromTree(catId, catIdList);
        int offSet = itemPerPage * (pageNo - 1);
        List<SchoolLinkEntity> sles = dao.getSchoolLinkByCat(catIdList, offSet, itemPerPage);
        return sles;
//    	List list = schoolLinkDao.getSchoolLinkByCat(catId, offSet, itemPerPage);
//    	return list;
    }

    //currentUrl is the url which leads to current page.
    public Page getPaginationByCat(int catId, int itemPerPage, String currentUrl) {
        int count = dao.getItemCountByCat(catId);

        String cleanUrl = currentUrl.replaceFirst("categoryId=\\d+&", "");
        Page page = new Page();
        page.setCommonUrl(cleanUrl);
        page.setItemPerPage(itemPerPage);
        page.setMaxItem(count);
        return new Page();

//    	return new Page(itemPerPage, count, cleanUrl);
    }

    @Override
    public int getMaxItemByCat(int catId) {
        ArrayList<Integer> catIdList = new ArrayList<Integer>();
        new CategoryBean().getListFromTree(catId, catIdList);

        int res = dao.getItemCountByCat(catIdList);
        return res;
    }

    @Override
    public Page getPagination() {
        return new Page();
    }

//    public getPaginationBylksdjfkd
    @Override
    public SchoolLinkEntity get(int id) {
        return dao.get(id);
    }

    @Override
    public SchoolLinkEntity add(SchoolLinkEntity entity) {
        if (entity.getLinkPicture().isEmpty()) {
            entity.setLinkPicture(new SchoolBean().get(entity.getSchoolId()).getLogo());
        }
        int newId = dao.add(entity);
        entity.setId(newId);
        return entity;
    }

    @Override
    public boolean delete(int id) {
        return dao.delete(id);
    }

    @Override
    public boolean update(SchoolLinkEntity entity) {
        return dao.update(entity);
    }

    @Override
    public List<SchoolLinkEntity> list() {
        return dao.list();
    }

    @Override
    public List<SchoolLinkEntity> search(String search, int itemPerPage, int pageNo) {
        int offSet = itemPerPage * (pageNo - 1);
        return dao.search(search, offSet, itemPerPage);

    }

    public int countSearch(String search) {
        return dao.countSearch(search);
    }
}
